home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / educate / wordy402.zip / RF.C < prev    next >
C/C++ Source or Header  |  1995-12-19  |  9KB  |  319 lines

  1. /**************************************************************************/
  2. /*                             Reformat Utility                           */
  3. /*                                                                        */
  4. /*                                 M\Cooper                               */
  5. /*                        3425 Chestnut Ridge Rd.                         */
  6. /*                        Grantsville, MD 21536-9801                      */
  7. /*                        --------------------------                      */
  8. /*                        Email:  thegrendel@aol.com                      */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package              */
  11. /*                                                                        */
  12. /*      Reformats the files created by the pattern matching utilities     */
  13. /*          so that they print in an "optimum" number of columns.         */
  14. /*                                                                        */
  15. /**************************************************************************/
  16.  
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <conio.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23.  
  24. #define FILE_OPENING_ERROR 3
  25. #define FILENAME_MAXLEN 8
  26. #define CR "\n"
  27. #define MAXLEN 82
  28. #define LINE_LEN 80
  29. #define NOARGS 1
  30. #define FULLARGS 3
  31. #define INCREMENT 1
  32. #define SPACE ' '
  33. #define MAXWORDLEN 24
  34. #define WORDSPACING 3
  35. //#define COLUMNS 5 (see below)
  36. #define BUFFERSIZE 4096
  37.  
  38. typedef enum { LESS_THAN, EQUAL, GREATER_THAN, ANOMALY } A_flag;
  39. typedef enum { OFF, ON } Mod_flag;
  40.  
  41.  
  42. void reformat( char *fname, int maxwordlength, int thr_wlen, A_flag action );
  43. A_flag parse_action( char *second_arg );
  44. int parse_threshhold ( char *second_arg );
  45. int get_max_word_length( char *fname );
  46. void center( char *strg ); 
  47.  
  48. char ad[] =
  49. "REFORMAT utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536";
  50.  
  51.  
  52.  
  53.  
  54. typedef enum { FALSE, TRUE } Boolean;
  55.  
  56. void main( int argc, char **argv )
  57. {
  58.  
  59.    char filename[ MAXLEN ];
  60.    int mwl,
  61.        threshhold = MAXWORDLEN;
  62.    A_flag action_flag;
  63.  
  64.      if( argc == NOARGS )
  65.         {
  66.         clrscr();
  67.         puts("Enter a FILENAME to reformat... ");
  68.         gets( filename );
  69.         }
  70.      else
  71.         strcpy( filename, *( argv + 1 ) );
  72.  
  73.       if( argc == FULLARGS )
  74.           {
  75.           action_flag = parse_action( *( argv + 2 ) );
  76.           threshhold =  parse_threshhold( *( argv + 2 ) );
  77.           }
  78.       else
  79.          {
  80.          action_flag = ANOMALY;
  81.          threshhold = MAXWORDLEN;
  82.          }
  83.  
  84.      mwl = get_max_word_length( filename );
  85.      reformat( filename, mwl, threshhold, action_flag );
  86. }
  87.  
  88.  
  89.  
  90.  
  91. /*************************************************************/
  92. void reformat( char *filename, int maxwl, int threshlen, A_flag action )
  93. {
  94.  
  95.     char    word [ MAXLEN ],
  96.       desc_messg [ MAXLEN ],
  97.       count_messg [ MAXLEN ],
  98.         tempstr [ MAXLEN + 3 ],
  99.         targetfile [ MAXLEN ],
  100.         tempfilename [ MAXWORDLEN ] = "tmp$$$.$$$",
  101.         bar [ LINE_LEN + 1 ],
  102.         double_bar [ LINE_LEN + 1 ];
  103.  
  104.     FILE *fptr,
  105.         *tfile;
  106.     long wcount = 0L;
  107.      int columns,
  108.          interval,
  109.          t_up = MAXWORDLEN,
  110.          t_down = 0;
  111.  
  112.  
  113.        memset( bar, '-', LINE_LEN );
  114.        *( bar + LINE_LEN ) = NULL;
  115.        memset( double_bar, '=', LINE_LEN );
  116.        *( double_bar + LINE_LEN ) = NULL;
  117.  
  118.        /*************opening credits*************/
  119.        clrscr();
  120.        printf( double_bar );
  121.        strcpy( tempstr, ad );
  122.        center ( tempstr );
  123.        printf( tempstr );
  124.        printf( CR );
  125.        printf( double_bar );
  126.        printf( CR );
  127.        /****************************************/
  128.  
  129.  
  130.        /*   Create name of temp file to store derived words in  */
  131.        /*********************************************************/
  132.        strcpy( targetfile, tempfilename );
  133.        /*********************************************************/
  134.  
  135.        if( !( fptr = fopen( filename, "rt" ) ) )
  136.          {
  137.          printf( "\7\7\7Cannot open file to reformat!" );
  138.          exit( FILE_OPENING_ERROR );
  139.          }
  140.       if( setvbuf( fptr, NULL, _IOFBF, BUFFERSIZE ) )
  141.          exit( FILE_OPENING_ERROR + 1 );
  142.  
  143.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  144.          {
  145.          printf( "\7\7\7Cannot open temp working file!" );
  146.          exit ( FILE_OPENING_ERROR + 2 );
  147.          }
  148.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  149.          exit( FILE_OPENING_ERROR + 3 );
  150.  
  151.        /**************'Wait' Message************/
  152.        printf( CR CR );
  153.        printf( "WORKING...\n\n" );
  154.        printf( "Reformatting file %s.\n", filename );
  155.        printf( "It may take a few seconds, depending on file length.\n\n" );
  156.        /*****************************************/
  157.  
  158.  
  159.          /*********************Check for word limits*********************/     
  160.  
  161.       threshlen += 1; //Compensate for CR at end of each word.
  162.  
  163.       switch ( action )
  164.              {
  165.              case EQUAL:
  166.                   t_up = threshlen + 1;
  167.                   t_down = threshlen - 1;
  168.                   sprintf( desc_messg, "List restricted to words exactly %d letters long.", threshlen - 1 );
  169.                   break;
  170.              case LESS_THAN:
  171.                   t_up = threshlen;
  172.                   t_down = 0;
  173.                   sprintf( desc_messg, "List restricted to words less than %d letters long.", threshlen - 1 );
  174.                   break;
  175.              case GREATER_THAN:
  176.                   t_up = MAXWORDLEN;
  177.                   t_down = threshlen;
  178.                   sprintf( desc_messg, "List restricted to words more that %d letters long.", threshlen - 1 );     
  179.                   break;
  180.              case ANOMALY:        //accept all...
  181.                   t_up = MAXWORDLEN;
  182.                   t_down = 0;
  183.              default: 
  184.                   break;
  185.  
  186.              }
  187.       maxwl = maxwl > t_up ? t_up : maxwl;
  188.       //Readjust column width, as necessary.
  189.  
  190.        columns = LINE_LEN / ( maxwl + WORDSPACING );
  191.       interval = maxwl + WORDSPACING;
  192.  
  193.          /*********************Main Loop*************/     
  194.  
  195.       printf( "\nThe file will be reformatted in %d columns.", columns );
  196.  
  197.           while( fgets( word, MAXLEN, fptr ) != NULL )
  198.  
  199.                               /*****888copy action888***/
  200.     if( strlen( word ) > t_down &&
  201.        strlen( word ) < t_up &&
  202.        !isspace( *word )  )
  203.  
  204.  
  205.                {
  206.       wcount++;
  207.  
  208.                *( word + strlen( word ) -1 ) = NULL;  //Gets rid of CR
  209.  
  210.                fprintf( tfile, "%-*s", interval, word );
  211.  
  212.                if( !( wcount % columns ) )
  213.                   fprintf( tfile, CR );
  214.                }
  215.             else
  216.     if( strlen( word)  >= MAXWORDLEN  && !iscntrl( *word ) )
  217.                fprintf( tfile, "\n%s", word );
  218.  
  219.      /**************999*************/
  220.  
  221.  
  222.       sprintf( count_messg, "From the original list, %ld words have been selected.", wcount );
  223.       center( count_messg );
  224.       fprintf( tfile, "\n\n%s", count_messg );
  225.       center ( desc_messg );
  226.       fprintf( tfile, "\n\n%s", desc_messg );
  227.  
  228.           /*******************************************/
  229.  
  230.           fcloseall();
  231.  
  232.           remove( filename );
  233.           rename( targetfile, filename );
  234.  
  235.           sprintf( tempstr,
  236.                  "The file %s has been reformatted.", filename );
  237.           center( tempstr );
  238.           printf( CR CR );
  239.           printf( tempstr );
  240.  
  241. }
  242.  
  243. int get_max_word_length( char *filename )
  244. {
  245.    int wlen,
  246.       maxwlen = 0; // Longest word in file
  247.    char word [ MAXLEN ];
  248.    FILE *fp;
  249.  
  250.       if( !( fp = fopen( filename, "rt" ) ) )
  251.             {
  252.             printf( "\7\7\7Cannot open file to reformat!" );
  253.             exit( FILE_OPENING_ERROR );
  254.             }
  255.  
  256.       clrscr();
  257.       printf( "Getting formatting information from file... \n" );
  258.       printf( "Please wait." );
  259.  
  260.       while( fgets( word, MAXLEN, fp ) != NULL )
  261.         {
  262.         wlen = strlen( word );
  263.         if( wlen > maxwlen && wlen < MAXWORDLEN )
  264.            maxwlen = wlen;  //Bump up to new value.
  265.         }
  266.  
  267.      return( maxwlen );
  268. }
  269.  
  270. void center( char *str )
  271. {
  272.    int padding;
  273.    char st [ LINE_LEN + INCREMENT ];
  274.  
  275.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  276.      memset( st, SPACE, padding );
  277.      *( st + padding ) = NULL;  //Terminate string
  278.      strcat( st, str );
  279.      strcpy( str, st );
  280.  
  281.      return;
  282. }
  283.  
  284. A_flag parse_action( char *arg2 )
  285. {
  286.  
  287.       switch ( *arg2 )
  288.          {
  289.          case 'g':
  290.          case 'G':
  291.          case 'm':
  292.          case 'M':
  293.          case 'O':
  294.          case 'o':
  295.               return( GREATER_THAN );
  296.          case 'l':
  297.          case 'L':
  298.          case 'f':
  299.          case 'F':
  300.          case 'U':
  301.          case 'u':
  302.          case 's':
  303.          case 'S':
  304.               return( LESS_THAN );
  305.         case 'e':
  306.         case 'E':
  307.         case '=':
  308.              return( EQUAL );
  309.         }
  310.  
  311.       return( ANOMALY );
  312. }
  313.  
  314. int parse_threshhold ( char *arg2b )
  315. {
  316.  
  317.       return( atoi( arg2b + 1 ) );
  318. }
  319.